package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.ProductModel;
import com.uwlaser.service.mapper.ProductModelMapper;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.ProductModelLien;
import com.uwlaser.service.ProductModelLienService;
import com.uwlaser.service.dto.ProductModelLienDto;
import com.uwlaser.service.dto.ProductModelLienQueryParam;
import com.uwlaser.service.mapper.ProductModelLienMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author zk-Qin
* @date 2022-05-31
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = ProductModelLienService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProductModelLienServiceImpl extends CommonServiceImpl<ProductModelLienMapper, ProductModelLien> implements ProductModelLienService {

    // private final RedisUtils redisUtils;
    private final ProductModelLienMapper productModelLienMapper;

    private final ProductModelMapper productModelMapper;

    @Override
    public PageInfo<ProductModelLienDto> queryAll(ProductModelLienQueryParam query, Pageable pageable) {
        IPage<ProductModelLien> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<ProductModelLien> page = productModelLienMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ProductModelLienDto.class);
    }

    @Override
    public List<ProductModelLienDto> queryAll(ProductModelLienQueryParam query){
        return ConvertUtil.convertList(productModelLienMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ProductModelLienDto.class);
    }

    @Override
    public ProductModelLien getById(Integer id) {
        return productModelLienMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public ProductModelLienDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), ProductModelLienDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProductModelLienDto resources) {
        ProductModelLien entity = ConvertUtil.convert(resources, ProductModelLien.class);
        return productModelLienMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ProductModelLienDto resources){
        ProductModelLien entity = ConvertUtil.convert(resources, ProductModelLien.class);
        int ret = productModelLienMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return productModelLienMapper.deleteBatchIds(ids);
    }

    /**
     * 根据产线工段获取产品
     * @param query
     * @return
     */
    @Override
    public List<ProductModel> getProductByLine(ProductModelLienQueryParam query) {
        List<ProductModelLienDto> modelList = queryAll(query);
        if(modelList == null || modelList.size() == 0){
            return new ArrayList<>();
        }
        Set<String> modelCodes = modelList.stream().map(ProductModelLienDto::getModelCode).collect(Collectors.toSet());
        QueryWrapper<ProductModel> wrapper = new QueryWrapper<ProductModel>().in("product_code", modelCodes);
        return  productModelMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<ProductModelLienDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ProductModelLienDto productModelLien : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品型号编码", productModelLien.getModelCode());
              map.put("产线编号编码", productModelLien.getLineCode());
              map.put("工段编号", productModelLien.getSectionCode());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
